home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 14590 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  1.5 KB

  1. Path: news.spies.com!usenet
  2. From: Erik Max Francis <max@alcyone.com>
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: 2 D arrays in C++ constructors
  5. Date: Sun, 31 Mar 1996 20:24:37 -0800
  6. Organization: Alcyone Systems
  7. Message-ID: <315F5A85.2E0DCBCF@alcyone.com>
  8. References: <4jlcn2$kvl@csugrad.cs.vt.edu>
  9. NNTP-Posting-Host: newton.alcyone.com
  10. Mime-Version: 1.0
  11. Content-Type: text/plain; charset=us-ascii
  12. Content-Transfer-Encoding: 7bit
  13. X-Mailer: Mozilla 2.01 (X11; I; Linux 1.2.13 i486)
  14.  
  15. Raj Suri wrote:
  16.  
  17. > This works fine.  However, now I need to use a two dimensional array in
  18. > which I don't know the dimensions until the constructor is called.  I can't
  19. > figure out how to declare ptr and allocate space to it such that I could do the
  20. > following statement:
  21. > ptr[2][3] = 4;
  22.  
  23. There's a few ways to do it.  The simplest is something like this (with X and Y
  24. being the size of the two-dimensional array):
  25.  
  26.     // First, allocate the first coordinate.
  27.     int **base = new int *[X];
  28.  
  29.     // Then for each first coordinate, allocate the corresponding second.
  30.     for (int x = 0; x < X; x++)
  31.         base[x] = new int[Y];
  32.  
  33. Then initialize the values as appropriate.
  34.  
  35. -- 
  36. Erik Max Francis &tSftDotIotE && http://www.alcyone.com/max && max@alcyone.com
  37. San Jose, California, U.S.A. && 37 20 07 N 121 53 38 W && the 4th R is respect
  38. H.3`S,3,P,3$S,#$Q,C`Q,3,P,3$S,#$Q,3`Q,3,P,C$Q,#(Q.#`-"C`- && 1love && folasade
  39. Omnia quia sunt, lumina sunt. && Dominion, GIGO, GOOGOL, Omega, Psi, Strategem
  40. "Out from his breast/his soul went to seek/the doom of the just." -- _Beowulf_
  41.